﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XpCtrl;
using System.Drawing;

public partial class Admin_bbsManage : System.Web.UI.Page
{
    Bbs bbsM;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {       
        if (!Page.IsPostBack)
        {
            this.Title = "云南希普防雷工程有限公司";
            SourceBind();
        }
    }

    protected void SourceBind() 
    {
        String strConn = ConfigurationManager.ConnectionStrings["xipuConnectionString"].ToString();
        bbsM = new Bbs(strConn);

        messageList.AllowPaging = true;
        messageList.PageSize = 15;
        messageList.Width = 395;
        messageList.BorderStyle = BorderStyle.Double;
        messageList.AutoGenerateDeleteButton = false;

        ds = bbsM.GetAllMessage();
        messageList.DataSource = ds;
        messageList.DataBind();

        LabTatalNo.Text = ds.Tables[0].Rows.Count.ToString();
        int buffer = messageList.PageIndex * 15+1;
        LabCurrentStart.Text = buffer.ToString();
        if (messageList.PageIndex * 15 + 15 >= ds.Tables[0].Rows.Count)
        {
            LabCurrentEnd.Text = ds.Tables[0].Rows.Count.ToString();
        }
        else
        {
            buffer = messageList.PageIndex * 15 + 15;
            LabCurrentEnd.Text = buffer.ToString();
        }
    }

    protected void messageList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        messageList.PageIndex = e.NewPageIndex;

        SourceBind();

    }

    protected void messageList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        #region 翻页绑定
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            Label label_Index = new Label();
            LinkButton Button_IndexFirst = new LinkButton();
            LinkButton Button_IndexLast = new LinkButton();
            LinkButton Button_IndexNext = new LinkButton();
            LinkButton Button_IndexPrevious = new LinkButton();

            Button_IndexFirst.Text = "第一页 ";
            Button_IndexFirst.Font.Size = 10;
            Button_IndexFirst.Font.Name = "黑体";
            Button_IndexFirst.CommandName = "first";
            Button_IndexFirst.ForeColor = Color.Black;
            Button_IndexFirst.Click += new EventHandler(PageButtonClick);

            Button_IndexNext.Text = "   下一页 ";
            Button_IndexNext.Font.Size = 10;
            Button_IndexNext.Font.Name = "黑体";
            Button_IndexNext.CommandName = "next";
            Button_IndexNext.ForeColor = Color.Black;

            Button_IndexNext.Click += new EventHandler(PageButtonClick);

            Button_IndexPrevious.Text = "前一页 ";
            Button_IndexPrevious.Font.Size = 10;
            Button_IndexPrevious.Font.Name = "黑体";
            Button_IndexPrevious.CommandName = "previous";
            Button_IndexPrevious.ForeColor = Color.Black;
            Button_IndexPrevious.Click += new EventHandler(PageButtonClick);

            Button_IndexLast.Text = "最末页 ";
            Button_IndexLast.Font.Size = 10;
            Button_IndexLast.Font.Name = "黑体";
            Button_IndexLast.CommandName = "last";
            Button_IndexLast.ForeColor = Color.Black;
            Button_IndexLast.Click += new EventHandler(PageButtonClick);

            label_Index.Text = "当前为第" + (messageList.PageIndex + 1) + "页,共有" + ((GridView)sender).PageCount + "页";
            label_Index.Font.Size = 8;
            e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(0, (Button_IndexFirst));
            e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(1, (Button_IndexPrevious));

            int controlTmp = e.Row.Controls[0].Controls[0].Controls[0].Controls.Count - 1;
            e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexNext);
            e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexLast);

            e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(label_Index);

            //e.Row.Controls[0].Controls.Add(label_Index);
        }
        #endregion
    }


    protected void PageButtonClick(object sender, EventArgs e)
    {
        LinkButton clickedButton = ((LinkButton)sender);
        if (clickedButton.CommandName == "first")
        {
            messageList.PageIndex = 0;
        }
        else if (clickedButton.CommandName == "next")
        {
            if (messageList.PageIndex < messageList.PageCount - 1)
            {
                messageList.PageIndex += 1;
            }
        }
        else if (clickedButton.CommandName == "previous")
        {
            if (messageList.PageIndex >= 1)
            {
                messageList.PageIndex -= 1;
            }
        }
        else if (clickedButton.CommandName == "last")
        {
            messageList.PageIndex = messageList.PageCount - 1;
        }
        SourceBind();
    }

    protected void messageList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteMessage") 
        {
            int index = Convert.ToInt32(e.CommandArgument);
            String strConn = ConfigurationManager.ConnectionStrings["xipuConnectionString"].ToString();
            bbsM = new Bbs(strConn);

            int n = bbsM.DeleteMessage(index);

            SourceBind();
            if (n == 0)
            {
                Response.Write("<script>alert('删除失败，请重新操作！');history.go(-1);</script>");
            }
        }
        else if(e.CommandName == "showMessage")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("showMsg.aspx?id="+index);
        }
    }
}
